package com.firmboy.search.netty.server;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

public class NettyServer {
	
	public static void main(String[] args) {
		//创建服务器
		ServerBootstrap bootstrap = new ServerBootstrap();
		//创建两个线程池
		ExecutorService boss = Executors.newCachedThreadPool();
		ExecutorService worker = Executors.newCachedThreadPool();
		
		bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));
		
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
			
			@Override
			public ChannelPipeline getPipeline() throws Exception {
				
				ChannelPipeline pipeline = Channels.pipeline();
				
				pipeline.addLast("decode", new StringDecoder());
				pipeline.addLast("encode", new StringEncoder());
				pipeline.addLast("handler", new Handler());
				
				return pipeline;
			}
		});
		
		bootstrap.bind(new InetSocketAddress(8000));
		
		System.out.println("start");
		
	}
}
